#include "memWordTab.h"

#define PHOTABKS_LEN 26	
static ushort phontableKS[30]={'C','R','B','E','Q','W','T','F','V','N','A','Z','J','I','5','7','9','^','\\','P','U','L','=','D','S','Y'};

static ushort phondestableKSUni[30]={0x006f,0x006f,0x0061,'2',0x039b,0x03b8,0x00f0,0x222b,'3',0x03b7,0x00e6,0x03b5,0x3072,'I',0x201a,0x00b8,0x201a,'g',0x0437,'u',0x0398,'2',0x00ea,0x00e3,0x00f5,0x03b5};

static void decodePhonetic(QString &retstr)
{
    ushort *srctab,*destab;
	srctab = phontableKS;
	destab = phondestableKSUni;
	int PHOTAB_LEN=PHOTABKS_LEN;
	int i,j;
	while((i=retstr.find('[',j))>-1)
	{
		j=retstr.find(']',i);
		for(int k=0;k<PHOTAB_LEN;k++){
			int l=retstr.find(QChar(srctab[k]),i);
			while ((l<j)&&(l>i)) 
			{
				retstr=retstr.replace(l,1,QChar(destab[k]));
				l=retstr.find(QChar(srctab[k]),l+1);
			}
		}
	}
}

memWordTab::memWordTab(QWidget *parent, QString dbName):ZWidget(parent)
{
	vOffset = 0;
	wd = new WordData(dbName);
	openKdict();
	engStr = wd->tmpwords[0].name;
	statue = 1;
	updateMean();
}
memWordTab::~memWordTab()
{
    delete wd;
}

void memWordTab::openKdict()
{
	initkdic();
	openkdic("/mmc/mmca1/qttest/dict/Oxford_for_zdic.pdb");
	engStr = QString("");
	updateMean();
}

void memWordTab::updateMean()
{
	char *eng;
	eng = searchword_general_kdic(un_unic(engStr), -1);
	meanStr = un_gbk(eng);
	decodePhonetic(meanStr);
	for(unsigned int i=0; i<meanStr.length()-1; i++)
	{
		QChar a = meanStr.at(i);
		if(a=='\\' && meanStr.at(i+1) == 'n') 
		{
			QString s=QString("\n");
			meanStr.replace(i,1,s);
			meanStr.remove(i+1,1);
		}
	}
	vOffset=0;
	this->repaint();
}

void memWordTab::paintEvent(QPaintEvent *e)
{
	p = QPixmap(this->width(), this->height());
	QPainter *pa = new QPainter();
	ZText zt = ZText(meanStr);
	QFont font = QFont("SSong");
	font.setPointSize(15);
	textHeight = zt.getWrapTextHeight(font, MEAN_HEIGHT, ZWrapLine::AtWhiteSpace);

	/* 画出翻译文本 */
	QPixmap textPix = QPixmap(MEAN_HEIGHT, textHeight);
	QRect rect = QRect(0, 0, MEAN_HEIGHT, textHeight);
	textPix.fill(QColor(255,255,255));
	pa->begin(&textPix);
		pa->setFont(font);
		zt.paint(*pa, rect, true, Qt::AlignLeft | Qt::AlignTop, QString("..."), 2, ZWrapLine::AtWhiteSpace);
	pa->end();
		

	QFont ori_font = pa->font();
	ori_font.setBold(true);
	ori_font.setPointSize(18);
	/* 画出英语 */
	p.fill(QColor(255,255,255));
	pa->begin(&p);
		pa->setFont(ori_font);
		pa->setPen(QColor(0,0,255));
		pa->drawText(0, 20, engStr);
		pa->setPen(QColor(0, 0, 0));
		//pa->drawText(0,50, phoneticStr);
		pa->drawLine(0,MEAN_Y - 1,240,MEAN_Y - 1);
		pa->setPen(QColor(230,130,50));/*橙色*/
		QString str=QString("%1/%2").arg(wd->memword+1).arg(wd->tmptotal);
		//printf("str=%s\n",str.latin1());
		pa->drawText(165,20,str);
		/* 画出进度条 */
		pa->setPen(QColor(0,255,0));
		pa->drawLine(237,MEAN_Y,237, MEAN_Y+MEAN_HEIGHT);
		pa->drawLine(239,MEAN_Y,239, MEAN_Y+MEAN_HEIGHT);
		pa->setPen(QColor(0,0,255));
		float per;
		if(textHeight < MEAN_HEIGHT)
			per = 1.0;
		else
			per = (vOffset*1.0/(textHeight-MEAN_HEIGHT));
		//printf("percentage = %f\n", per);
		int lenPercent = MEAN_HEIGHT*per;
		//printf("lenPercent = %d\n", lenPercent);
		pa->drawLine(238,MEAN_Y,238, MEAN_Y+lenPercent);

		//画底线
		pa->setPen(QColor(0,0,0));
		pa->drawLine(0, MEAN_Y + MEAN_HEIGHT+1, 240, MEAN_Y + MEAN_HEIGHT+1);
	pa->end();
	delete pa;

	/* 将翻译复制至buffer中 */
	if(statue ==2 || statue == 3)
		bitBlt(&p, 0, MEAN_Y, &textPix, 0, vOffset, MEAN_WIDTH, MEAN_HEIGHT-2);

	/* 将buffer显示到屏幕 */
	bitBlt(this,0,0,&p);
}

void memWordTab::PressEvent(QKeyEvent *e)
{
	int kvalue = e->key();
	if(kvalue == KEY_DOWN || kvalue == KEY_8)
	{
		if(textHeight <= MEAN_HEIGHT) return;
		if(vOffset+OFFSET_STEP < textHeight-MEAN_HEIGHT)
			vOffset+=OFFSET_STEP;
		else
			vOffset = textHeight-MEAN_HEIGHT;
		this->repaint();
		return;
	}
	if(kvalue == KEY_UP || kvalue == KEY_2)
	{
		if(vOffset-OFFSET_STEP>= 0)
			vOffset-=OFFSET_STEP;
		else
			vOffset=0;
		this->repaint();
		return;
	}
}

void memWordTab::ReleaseEvent(QKeyEvent *e)
{
	int kvalue = e->key();
	if(kvalue == KEY_0)
	{
		vOffset = 0;
		this->repaint();
		return;
	}
	if(kvalue == KEY_1 || kvalue == KEY_4 || kvalue == KEY_7 || kvalue == KEY_5 || kvalue == KEY_CENTER)
		yesFunc();
	if(kvalue == KEY_3 || kvalue == KEY_6 || kvalue == KEY_9)
		noFunc();
}

void memWordTab::yesFunc()
{
	//printf("yes\n");
	switch(statue)
	{
		case 1:
			statue=2;
			this->repaint();
			break;
		case 2:
			statue=1;
			wd->changeWord(true);
			engStr = wd->tmpwords[0].name;
			updateMean();
			break;
		case 3:
			statue=1;
			wd->changeWord(false);
			engStr = wd->tmpwords[0].name;
			updateMean();
			break;
	}
	emit updateSoftKey();
}

void memWordTab::noFunc()
{
	switch(statue)
	{
		case 1:
			statue=3;
			this->repaint();
			break;
		case 2:
			statue=1;
			wd->changeWord(false);
			engStr = wd->tmpwords[0].name;
			updateMean();
			break;
		case 3:
			statue=1;
			wd->changeWord(false);
			engStr = wd->tmpwords[0].name;
			updateMean();
			break;
	}
	//printf("no and statue change to %d\n", statue);
	emit updateSoftKey();
}

